<template>
  <div>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="handleAddCate">添加分类</el-button>
        </el-col>
      </el-row>

      <tree-table
        :data="cateList"
        :columns="columns"
        show-index
        index-text="#"
        border
        :expand-type="false"
        :selection-type="false"
      >
        <!-- 是否有效 -->
        <template slot="isAvailble" slot-scope="scoped">
          <i
            class="el-icon-success"
            v-if="scoped.row.cat_deleted === false"
            style="color: lightgreen"
          ></i>
          <i class="el-icon-error" v-else style="color:red"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scoped">
          <el-tag size="mini" v-if="scoped.row.cat_level === 0">一级</el-tag>
          <el-tag size="mini" type="success" v-else-if="scoped.row.cat_level === 1">二级</el-tag>
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope="scoped">
          <el-button icon="el-icon-edit" type="primary" size="mini">编辑</el-button>
          <el-button icon="el-icon-delete" type="danger" size="mini">删除</el-button>
        </template>
      </tree-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.pagenum"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="pageInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 对话框 -->
    <el-dialog title="添加分类" :visible.sync="isAddCate" width="50%" @close="closeDialog">
      <el-form :model="addCate" :rules="addCateRules" ref="addCateRef" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCate.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            v-model="selectedKeys"
            :options="parentCateList"
            :props="propsConfig"
            change-on-select
            clearable
            expand-trigger="hover"
            @change="hanldeCascader"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isAddCate = false">取 消</el-button>
        <el-button type="primary" @click="addCateForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cateList: [],
      total: 0,
      pageInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          prop: '',
          type: 'template',
          template: 'isAvailble'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      isAddCate: false,
      addCate: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      addCateRules: {
        cat_name: [{ required: true, message: '请输入名称', trigger: 'blur' }]
      },
      parentCateList: [],
      propsConfig: {
        value: 'cat_id', //值
        label: 'cat_name', //标签
        children: 'children' //儿子
      },
      selectedKeys: []
    }
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.pageInfo
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.cateList = res.data.result
      this.total = res.data.total
      console.log(res.data)
    },
    handleSizeChange(newSize) {
      this.pageInfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange(newPage) {
      this.pageInfo.pagenum = newPage
      this.getCateList()
    },
    handleAddCate() {
      this.getParentCateList()
      this.isAddCate = true
    },
    async getParentCateList() {
      const { data: res } = await this.$http('categories', {
        params: { type: 2 }
      })

      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.parentCateList = res.data
    },
    hanldeCascader() {
      if (this.selectedKeys.length > 0) {
        this.addCate.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        this.addCate.cat_level = this.selectedKeys.length
      } else {
        this.addCate.cat_pid = 0
        this.addCate.cat_level = 0
      }
      console.log(this.selectedKeys)
    },
    addCateForm() {
      // console.log("-------",this.addCate)
      this.$refs.addCateRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('categories', this.addCate)
        if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        this.getCateList()
        this.isAddCate = false
      })
    },
    closeDialog() {
      this.$refs.addCateRef.resetFields()
      this.selectedKeys = []
      this.addCate.cat_level = 0
      this.addCate.cat_pid = 0
    }
  },
  created() {
    this.getCateList()
  }
}
</script>

<style scoped lang="less">
.el-cascader {
  width: 100%;
}
</style>